package com.vbobot.demo.spring.nacos.client;

import feign.RequestInterceptor;
import java.io.IOException;
import java.util.Enumeration;
import javax.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpRequest;
import org.springframework.http.client.ClientHttpRequestExecution;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

/**
 * 传递请求头
 * @author xiaoxin
 */
@Slf4j
@Configuration
public class HeaderTransmitConfig {

    /**
     * 解决fein远程调用丢失请求头
     * @return RequestInterceptor
     */
    @Bean("XHeaderRequestInterceptor")
    public RequestInterceptor requestInterceptor() {
        return template -> {
            // 1RequestContextHolder 拿到当前的请求
            ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
            if (attributes != null) {
                HttpServletRequest request = attributes.getRequest();
                Enumeration<String> headerNames = request.getHeaderNames();
                if (headerNames != null) {
                    while (headerNames.hasMoreElements()) {

                        String name = headerNames.nextElement();
                        if (name.startsWith("x-")) {
                            // 传递所有x-开头的请求头, 如x-user-ua
                            String values = request.getHeader(name);
                            template.header(name, values);
                        }
                    }
                }
            }
        };
    }

    /**
     * RestTempalate 传递请求头
     */
    public static class HeaderRequestInterceptor implements ClientHttpRequestInterceptor {
        @Override
        public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
            // 1RequestContextHolder 拿到当前的请求
            ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
            if (attributes != null) {
                HttpServletRequest sourceRequest = attributes.getRequest();
                Enumeration<String> headerNames = sourceRequest.getHeaderNames();
                if (headerNames != null) {
                    while (headerNames.hasMoreElements()) {
                        String name = headerNames.nextElement();
                        if (name.startsWith("x-")) {
                            // 传递所有x-开头的请求头, 如x-user-ua
                            String values = sourceRequest.getHeader(name);
                            request.getHeaders().set(name, values);
                        }
                    }
                }
            }
            return execution.execute(request, body);
        }
    }

}
